﻿using System;
using Microsoft.SPOT;

#if WINDOWS
using System.Runtime.ExceptionServices;
using System.Windows;
using System.Windows.Threading;

#endif

#if NETMF

#endif

namespace Coding4Fun.Maelstrom.Hardware.Diagnostics
{
	public static class Logger
	{
		private const string DefaultLog = "Maelstrom Log";
#if WINDOWS
		private static readonly string ProcessName = Process.GetCurrentProcess().ProcessName;
#else
		private static readonly string ProcessName = "Phone/NETMF";
#endif

		public static void WriteLine(object item)
		{
			WriteLine(ProcessName, item, false);
		}

		public static void WriteLine(object item, bool unhandled)
		{
			WriteLine(ProcessName, item, unhandled);
		}

		public static void WriteLine(string source, object item)
		{
			WriteLine(source, item, false);
		}

		public static void WriteLine(string source, object item, bool unhandled)
		{
#if !DEBUG
			return;
#endif
			try
			{
				string msg = FormatMessage(item);
#if NETMF
				Debug.Print(msg);
#elif WINDOWS_PHONE
				Debug.WriteLine(msg);
#elif WINDOWS
				Trace.WriteLine(msg);
				WriteEventLogEntry(source, msg, unhandled);
#endif
			}
			catch(Exception ex)
			{
				string exMsg = "******* Error in Logger: " + ex;
#if NETMF
				Debug.Print(exMsg);
#elif WINDOWS_PHONE
				Debug.WriteLine(exMsg);
#elif WINDOWS
				Trace.WriteLine(exMsg);
				Debug.WriteLine(exMsg);
				Console.Write(exMsg);
#endif
			}
		}

#if WINDOWS
		private static void WriteEventLogEntry(string source, string item, bool unhandled)
		{
//			if(!EventLog.SourceExists(source))
//				EventLog.CreateEventSource(source, DefaultLog);

			EventLog.WriteEntry("Detroit"/*source*/, item, unhandled ? EventLogEntryType.Error : EventLogEntryType.Information);
		}
#endif

		private static string FormatMessage(object msg)
		{
#if NETMF
			return DateTime.Now + " - NETMF: " + msg;
#elif WINDOWS_PHONE
			return string.Format("{0} - Phone: {1}", DateTime.Now, msg);
#else
			return string.Format("{0} - {1} - {2}: {3}", DateTime.Now, Environment.MachineName, ProcessName, msg);
#endif
		}

#if WINDOWS
		public static void SetupLogging(Application app)
		{
			app.DispatcherUnhandledException += (s,e) => { WriteLine(e.Exception); e.Handled = false; };
			SetupLogging();
		}
#endif

		public static void SetupLogging()
		{
#if WINDOWS
			AppDomain.CurrentDomain.UnhandledException += (s,e) => 
			{
				//Exception ex = (Exception)e.ExceptionObject;
				//if(ex.StackTrace.Contains("WriteLine"))
				//{
				//    Debug.WriteLine("Incoming stack overflow...");
				//    Debugger.Break();
				//    return;
				//}
				WriteLine(e.ExceptionObject, true);
			};
			AppDomain.CurrentDomain.FirstChanceException += (s,e) =>
			{
				//if(e.Exception.StackTrace.Contains("WriteLine"))
				//{
				//    Debug.WriteLine("Incoming stack overflow...");
				//    Debugger.Break();
				//    return;
				//}
				WriteLine(e.Exception, true);
			};
#endif
		}
	}
}
